package io.github.talelin.latticy.common.util;

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @ClassName：UnPackeUtil
 * @Author: ljj
 * @Date: 2022/5/12 15:13
 */
@Slf4j
public class UnPackeUtil {

    public final static String ZIP_FILE = "zip";

    /**
     * rar文件解压(不支持有密码的压缩包)
     *
     * @param rarAsStream rar压缩包
     */
    public static List<MultipartFile> unPackRar(InputStream rarAsStream) {
        List<MultipartFile> multipartFiles = new ArrayList<>();
        try (Archive archive = new Archive(rarAsStream)) {
            FileHeader fileHeader = archive.nextFileHeader();
            while (null != fileHeader) {
                if (!fileHeader.isDirectory()) {
                    String fileNameString = fileHeader.getFileNameString();
                    byte[] subData = fileHeader.getSubData();
                    MultipartFile file = new MockMultipartFile(fileNameString, fileNameString, "", subData);
                    multipartFiles.add(file);
                }
                fileHeader = archive.nextFileHeader();
            }
        } catch (Exception e) {
            log.error("解压失败：", e.getMessage(), e);
        }
        return multipartFiles;
    }

    /**
     * 获取zip压缩包下的文件信息
     *
     * @param zipFile
     * @return
     * @throws IOException
     */
    public static List<MultipartFile> zip(MultipartFile zipFile) throws IOException {
        List<MultipartFile> multipartFiles = new ArrayList<>();
        ZipInputStream zis = new ZipInputStream(zipFile.getInputStream());
        ZipEntry zipEntry;
        //循环遍历
        try {
            while ((zipEntry = zis.getNextEntry()) != null) {
                String name = zipEntry.getName();
                //指定文件的字节数组
                byte[] bs = getZipEntryData(zis);
                if (!zipEntry.isDirectory()) {
                    MultipartFile file = new MockMultipartFile(name, name, "", bs);
                    multipartFiles.add(file);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (zis != null) {
                zis.closeEntry();
            }
        }
        return multipartFiles;
    }

    private static byte[] getZipEntryData(InflaterInputStream zis) {
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            int length;
            while ((length = zis.read(temp, 0, 1024)) != -1) {
                bout.write(temp, 0, length);
            }
            byte[] buf = bout.toByteArray();
            bout.close();
            return buf;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
